#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROWS 10
#define COLS 10
#define MINES 20

int board[ROWS][COLS];
int revealed[ROWS][COLS];

void initializeBoard() {
    int i, j;
    for (i = 0; i < ROWS; i++) {
        for (j = 0; j < COLS; j++) {
            board[i][j] = 0;
            revealed[i][j] = 0;
        }
    }
}

void placeMines() {
    int minesPlaced = 0;
    while (minesPlaced < MINES) {
        int row = rand() % ROWS;
        int col = rand() % COLS;
        if (board[row][col] != -1) {
            board[row][col] = -1;
            minesPlaced++;

            // Increment adjacent cells
            for (int i = -1; i <= 1; i++) {
                for (int j = -1; j <= 1; j++) {
                    int newRow = row + i;
                    int newCol = col + j;
                    if (newRow >= 0 && newRow < ROWS && newCol >= 0 && newCol < COLS && board[newRow][newCol] != -1) {
                        board[newRow][newCol]++;
                    }
                }
            }
        }
    }
}

void printBoard() {
    int i, j;
    printf("   ");
    for (j = 0; j < COLS; j++) {
        printf("%d ", j);
    }
    printf("\n");

    for (i = 0; i < ROWS; i++) {
        printf("%d: ", i);
        for (j = 0; j < COLS; j++) {
            if (revealed[i][j]) {
                if (board[i][j] == -1) {
                    printf("* ");
                }
                else {
                    printf("%d ", board[i][j]);
                }
            }
            else {
                printf(". ");
            }
        }
        printf("\n");
    }
}

int revealCell(int row, int col) {
    if (row < 0 || row >= ROWS || col < 0 || col >= COLS || revealed[row][col]) {
        return 0;
    }

    revealed[row][col] = 1;
    if (board[row][col] == -1) {
        return -1; // Hit a mine
    }

    if (board[row][col] == 0) {
        // Recursively reveal adjacent cells
        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
                if (i != 0 || j != 0) {
                    revealCell(row + i, col + j);
                }
            }
        }
    }

    return 0;
}

int main() {
    srand(time(NULL));
    initializeBoard();
    placeMines();

    int row, col;
    int gameover = 0;
    while (!gameover) {
        printBoard();
        printf("Enter row and column to reveal: ");
        scanf("%d %d", &row, &col);

        if (revealCell(row, col) == -1) {
            printf("Game Over! You hit a mine!\n");
            gameover = 1;
        }
        else {
            // Check if all non-mine cells are revealed
            gameover = 1;
            for (int i = 0; i < ROWS; i++) {
                for (int j = 0; j < COLS; j++) {
                    if (board[i][j] != -1 && !revealed[i][j]) {
                        gameover = 0;
                        break;
                    }
                }
                if (!gameover) {
                    break;
                }
            }

            if (gameover) {
                printf("Congratulations! You've cleared the minefield!\n");
            }
        }
    }

    // Reveal the entire board
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            revealed[i][j] = 1;
        }
    }
    printBoard();

    return 0;
}
